package cn.jaye.fabric.controller;

import cn.jaye.fabric.application.ChainCodeApplicationService;
import cn.jaye.fabric.application.command.chaincode.InstallChainCodeCommand;
import cn.jaye.fabric.application.command.chaincode.InstantiateChainCodeCommand;
import cn.jaye.fabric.application.command.chaincode.InvokeChainCodeCommand;
import cn.jaye.fabric.application.command.chaincode.QueryChainCodeCommand;
import cn.jaye.fabric.common.constants.Global;
import cn.jaye.fabric.common.util.ResultUtil;
import cn.jaye.fabric.domain.model.User;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Jaye
 * @create 2018-05-02 11:45
 */
@RestController
@RequestMapping("chaincode")
public class ChainCodeController {

    @Autowired
    private ChainCodeApplicationService chainCodeApplicationService;

    @PostMapping(value = "install")
    public String install(HttpServletRequest request, @RequestBody InstallChainCodeCommand installChainCodeCommand) throws Exception {
        chainCodeApplicationService.install(installChainCodeCommand);
        return ResultUtil.success();
    }

    @PostMapping(value = "instantiate")
    public String instantiate(HttpServletRequest request, @RequestBody InstantiateChainCodeCommand instantiateChainCodeCommand) throws Exception {
        chainCodeApplicationService.instantiate(instantiateChainCodeCommand);
        return ResultUtil.success();
    }

    @PostMapping(value = "invoke")
    public String invoke(HttpServletRequest request, @RequestBody InvokeChainCodeCommand invokeChainCodeCommand) throws Exception {
        return ResultUtil.success(chainCodeApplicationService.invoke(invokeChainCodeCommand));
    }

    @PostMapping(value = "query")
    public String query(HttpServletRequest request, @RequestBody QueryChainCodeCommand queryChainCodeCommand) throws Exception {
        return ResultUtil.success(chainCodeApplicationService.query(queryChainCodeCommand));
    }
}
